<?php
/**
 * admin模型
 * @author 
 * @version 1.0
 * @package application
 * @subpackage application/models
 */

class Admin_Model extends MY_Model
{
 
	public function __construct(){
		parent::__construct();
		$this->_pk='admin_id';
		$this->_attributes=array( 
				'admin_id'=>'',
				'group_id'=>'',
				'user_name'=>'',
				'real_name'=>'',
				'password'=>'',
				'email'=>'',
				'description'=>'',
				'begin_time'=>'',
				'end_time'=>'',
				'login_count'=>'',
				'last_login_time'=>'',
				'last_login_ip'=>'',
				'created'=>'',
				'modified'=>'',
				'status'=>'',
                'phone' =>'',
				'login_method'=>'',
		);
	}
	public $_table = array(
		'a' => 'admin',
	);
	
	
	/**
	 * 检查用户是否通过验证
	 * 
	 * @access public
	 * @param string - $name 用户名
	 * @param string - $password 密码
	 * @return mixed - FALSE/uid
	 */	
	public function validate_user($username, $password)
	{
		$data = FALSE;

		$this->db->where('user_name', $username);
		$query = $this->db->get($this->table_name());

		if($query->num_rows() == 1)
		{
			$data = $query->row_array();		
		}

		if(!empty($data))
		{
			$data = (md5($password)==$data['password']) ? $data : FALSE;
		}

		$query->free_result();

		return $data;
	}

	/**
	 * 登陆成功后的更新函数
	 * @param  string $field      [description]
	 * @param  [type] $data       [description]
	 * @param  [type] $attributes [description]
	 * @return [type]             [description]
	 */
	public function login_success_update($field='login_count' , $data , $attributes){
		//$sql = "UPDATE {$this->_table['a']} SET `$field` = `$field` +1, `last_login_ip` = '{$data['last_login_ip']}', `last_login_time` = '{$data['last_login_time']}' WHERE `user_name` = '{$attributes['user_name']}' AND `password` = '{$attributes['password']}' ";
		$sql = "UPDATE {$this->_table['a']} SET $field = $field + 1, last_login_ip = '{$data['last_login_ip']}', last_login_time = '{$data['last_login_time']}' WHERE user_name = '{$attributes['user_name']}' AND password = '{$attributes['password']}' ";
		$query = $this->db->query($sql);
		return $query;	
	}

}